package com.wck.car.car;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wck.car.car.service.AppCarBrandService;
import com.wck.car.model.AppCarBrand;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;

import javax.annotation.Resource;

@Component("JobInfoDaoPipeline")
public class JobInfoDaoPipeline implements Pipeline {

    @Resource
    private AppCarBrandService carBrandService;

    @Override
    public void process(ResultItems resultItems, Task task) {
        String str = resultItems.get("repo");
        JsonRootBean jsonRootBean = JSON.parseObject(str, JsonRootBean.class);
        if (jsonRootBean.getResult().getBranditems().size() > 0) {
            jsonRootBean.getResult().getBranditems().forEach(bean -> {
                if (carBrandService.count(new LambdaQueryWrapper<AppCarBrand>().eq(AppCarBrand::getId, bean.getId())) < 1) {
                    AppCarBrand appCarBrand = new AppCarBrand();
                    BeanUtils.copyProperties(bean, appCarBrand);
                    appCarBrand.setId(Integer.parseInt(bean.getId()));
                    carBrandService.save(appCarBrand);
                    System.out.println("保存成功");
                } else {
                    System.out.println("数据已存在");
                }
            });
        }
    }
}
